package LeetCode.leetcode257;

import java.util.ArrayList;
import java.util.List;

public class Review {
    public static void main(String[] args) {
        String s = "012345678";
        System.out.println(s.substring(1, s.length()));
    }

    public List<String> binaryTreePaths(TreeNode root) {
        List<String> res = new ArrayList<>();
        StringBuilder path = new StringBuilder();
        test(res, root, path);
        for (int i = 0; i < res.size(); i++) {
            String tmp = res.get(i);
            if (tmp.length() != 0) {
                res.set(i, tmp.substring(0,tmp.length()-2));
            }
        }
        return res;
    }

    public static void test(List<String> res, TreeNode root, StringBuilder path) {
        path.append(root.val);
        path.append("->");
        if (root.left == null && root.right == null) {
            res.add(path.toString());
            return;
        }
        if (root.left != null) {
            test(res, root.left, path);
            path.deleteCharAt(path.length() - 1);
            path.deleteCharAt(path.length() - 1);
        }
        if (root.right != null) {
            test(res, root.right, path);
            path.deleteCharAt(path.length() - 1);
            path.deleteCharAt(path.length() - 1);
        }
    }
}
